/**
 * DifferUpdateHelper
 */

  var DifferUpdateHelper = (function (){
      var dataProvider;
      var differs = [];

      var getIndexById = function (id){
          for (var i=0,l=differs.length; i<l; i++){
              if (differs[i].id === id){
                  return i;
              }
          }
          return -1;
      };

      var doSave = function (){
          if (differs.length){
              dataProvider.batchUpdateWidgets(differs);
          }
      };

      var doBatch = function (data){
          if (!data.id) return;
          var index = getIndexById(data.id);
          if (index === -1){
              differs.push(data);
          } else {
              differs.splice(index, 1, data);
          }
      };

      var doInject = function (param){
          dataProvider = param.dataProvider;
      };

      return {
          save: doSave,
          batch: doBatch,
          inject: doInject
      }
  })();

  module.exports = DifferUpdateHelper;
